﻿using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using HopNguyenCms.App_Start;
using HopNguyenCms.Infrastructure.Attributes;

namespace HopNguyenCms
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            MvcHandler.DisableMvcResponseHeader = true;
            GlobalConfiguration.Configuration.Filters.Add(new ElmahErrorAttribute());
        }

        protected void Application_BeginRequest(object sender, EventArgs ev)
        {
            var fromHomeURL = "http://getlinkyoutube.com";
            var toHomeURL = "http://www.getlinkyoutube.com";

            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(fromHomeURL))
            {
                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location",
                Request.Url.ToString().ToLower().Replace(fromHomeURL, toHomeURL));
            }
        }

        void Application_Error(object sender, EventArgs e)
        {
            var ex = HttpContext.Current.Server.GetLastError();
            var httpException = ex as HttpException;
            if (httpException != null)
                Response.Redirect("/error.html");
        }
    }
}